package stack

import (
	C "gitee.com/ljfirst/algo-go-sdk/common/constant"
)

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/6/28 19:06
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type MinStack struct {
	ArrayStack
	minStack ArrayStack
}

func (m *MinStack) Push(value int) {
	m.ArrayStack.Push(value)
	if !m.minStack.Empty() && m.minStack.Peak().(int) < value {
		m.minStack.Push(m.minStack.Peak())
		return
	}
	m.minStack.Push(value)
}

func (m *MinStack) Pop() interface{} {
	if m.ArrayStack.Empty() {
		return C.ErrorNum
	}
	m.minStack.Pop()
	return m.ArrayStack.Pop()
}

func (m *MinStack) Peak() interface{} {
	if m.ArrayStack.Empty() {
		return C.ErrorNum
	}
	m.minStack.Peak()
	return m.ArrayStack.Peak()
}

func (m *MinStack) GetMin() interface{} { return m.minStack.Peak() }

func (m *MinStack) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{C.MinStack},
		Desc: &C.Desc{
			Name:   "MinStack",
			NameCn: "最小栈",
			ParamsDesc: map[string]string{
				"ArrayStack": "内部维护一个数组来控制先进后出",
				"minStack":   "内部维护一个数组，存储当前栈的最小值",
			},
		},
	}
}
